﻿using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace QQSDK
{

    public class FakeAds : MonoBehaviour
    {
        public static FakeAds instance;

        public GameObject TopBanner;
        public GameObject BottomBanner;

        public GameObject Interstitial;
        public GameObject RewardVideo;

        public Text rewardVideoCountTimeText;

        public int rewardVideoTimeLength = 5;

        public bool isCompleteWatchRewardVideo;
        private bool isStop;

        void Start()
        {
            if (instance == null)
            {
                instance = this;
            }
        }
        public void ShowBanner(bool top = false)
        {

            if (top)
            {
                TopBanner.SetActive(true);
            }
            else
            {
                BottomBanner.SetActive(true);
            }
            AdsManager.instance.onBannerImpression();
        }
        public void HideBanner()
        {
            if (TopBanner.activeInHierarchy)
            {
                TopBanner.SetActive(false);
            }
            if (BottomBanner.activeInHierarchy)
            {
                BottomBanner.SetActive(false);
            }
        }
        public void ShowInterstitial()
        {
            Interstitial.SetActive(true);
            AdsManager.instance.onInterstitialOpen();
        }
        public void OnCloseInterstitial()
        {
            Interstitial.SetActive(false);
            AdsManager.instance.onInterstitialClose();
            //TimerSchedule.Schedule(this, Random.Range(0, 10), () =>
            //{
            //    AdsManager.instance.onInterstitialLoaded();
            //});

            Invoke("onInterstitialLoaded", Random.Range(0, 10));
        }
        public void onInterstitialLoaded()
        {
            AdsManager.instance.onInterstitialLoaded();
        }
        public void ShowRewardVideo()
        {
            RewardVideo.SetActive(true);
            AdsManager.instance.onRewardOpen();
            StartCoroutine(StartCountDownTime());
        }
        IEnumerator StartCountDownTime()
        {
            isCompleteWatchRewardVideo = false;
            int time = rewardVideoTimeLength;
            while (time > 0)
            {
                if (isStop)
                    yield return null;
                rewardVideoCountTimeText.text = time.ToString();
                time--;
                yield return new WaitForSeconds(1f);
            }
            if (time == 0)
            {
                rewardVideoCountTimeText.text = "";
                isCompleteWatchRewardVideo = true;
            }
        }
        public void OnClickCloseRewardVideo()
        {
            RewardVideo.SetActive(false);
            isStop = !isCompleteWatchRewardVideo;
            if (isStop)
            {
                AdsManager.instance.onRewardInterrupt();
            }
            else
            {
                AdsManager.instance.onRewardComplete();
            }
            //模拟广告加载成功的逻辑
            //TimerSchedule.Schedule(this, Random.Range(0, 10), () =>
            //{
            //    AdsManager.instance.onRewardLoaded();
            //});

            Invoke("onRewardLoaded", Random.Range(0, 10));
        }


        public void onRewardLoaded()
        {
            AdsManager.instance.onRewardLoaded();
        }

        public void ShowNative(Vector2 where)
        {
           
        }
        public void HideNative()
        {
           
        }
    }
}